<?php

namespace app\admin\controller\renovation;

use app\admin\controller\Base;
use app\common\model\renovation\CategoryModel;

class Category extends Base
{
    /**
     * 获取分类数树
     */
    public function getCategoryTree()
    {
        $data = (new CategoryModel)->field('id,parent_id,sort,name')
            ->order('sort', 'DESC')
            ->select()->toArray();
        return response_success('', get_tree($data, 0, 'parent_id'));
    }

    /**
     * 获取所有父级分类
     */
    public function getParentCategory()
    {
        $data = (new CategoryModel)->field('id,name')
            ->where('parent_id', 0)
            ->select()->toArray();

        return response_success('', $data);
    }

    /**
     * 添加分类
     */
    public function add()
    {
        $params = request()->only(['parent_id', 'name', 'sort']);
        $this->validate($params, [
            'name|名称' => 'require',
        ]);

        if (isset($params['parent_id']) && !empty($params['parent_id'])) {
            $categoryInfo = (new CategoryModel)->where('id', $params['parent_id'])->find();
            if (!$categoryInfo) {
                return response_error('父级分类信息不存在或已删除');
            }
        }

        if ((new CategoryModel)->where('name', $params['name'])->find()) {
            return response_error('分类名称已存在');
        }

        (new CategoryModel)->insert([
            'parent_id' => $params['parent_id'] ?? 0,
            'name' => $params['name'],
            'sort' => $params['sort'],
            'create_admin_id' => $this->adminInfo['id'],
            'create_time' => date('Y-m-d H:i:s')
        ]);

        return response_success();
    }

    /**
     * 删除分类
     */
    public function del()
    {
        $params = request()->only(['ids']);
        $this->validate($params, [
            'ids' => 'require'
        ], [
            'ids.require' => '请选择删除分类'
        ]);

        $categoryModel = new CategoryModel;
        if (!$categoryModel->delCategory($params['ids'])) {
            return response_error($categoryModel->getError());
        }

        return response_success();
    }

    /**
     * 编辑分类
     */
    public function edit()
    {
        $params = request()->only(['id', 'parent_id', 'name', 'sort']);
        $this->validate($params, [
            'id|ID' => 'require',
            'name|名称' => 'require',
        ]);

        if (isset($params['parent_id']) && !empty($params['parent_id'])) {
            $categoryInfo = (new CategoryModel)->where('id', $params['parent_id'])->find();
            if (!$categoryInfo) {
                return response_error('父级分类信息不存在或已删除');
            }
        }

        if (isset($params['parent_id']) && $params['parent_id'] == $params['id']) {
            response_error('父级分类不能选择自己');
        }

        if ((new CategoryModel)->where('id', '<>', $params['id'])->where('name', $params['name'])->find()) {
            return response_error('分类名称已存在');
        }

        (new CategoryModel)->where('id', $params['id'])->update([
            'parent_id' => $params['parent_id'] ?? 0,
            'name' => $params['name'],
            'sort' => $params['sort'],
            'update_time' => date('Y-m-d H:i:s')
        ]);

        return response_success();
    }

}